package qc.module.qms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.common.core.unify.QCUnifyReturnValue;
import qc.module.qms.entity.QmsStationDept;
import qc.module.qms.repository.QmsStationDeptRepository;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class QmsStationDeptService {
    
    @Autowired
    private QmsStationDeptRepository repository;

    /**
     * 获取指定站点关联的部门ID集合
     *
     * @param stcd 站点编码
     * @return 站点与部门关联关系
     * @author QuCheng Tech
     * @since 2024/5/6
     */
    public List<Integer> getDeptIdsByStcd(String stcd) {
        LambdaQueryWrapper<QmsStationDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(QmsStationDept::getDid);
        wrapper.eq(QmsStationDept::getStcd, stcd);

        //查询数据
        List<QmsStationDept> ens = repository.selectList(wrapper);
        if (ens != null && ens.size() > 0x0) {
            return ens.stream().map(QmsStationDept::getDid).collect(Collectors.toList());
        }

        return null;
    }

    /**
     * 删除指定站点与部门关联关系
     *
     * @param stcd 站点编码
     * @author 成功返回null
     * @since 2024/5/6
     */
    public void deleteDeptsByStcd(String stcd) {
        LambdaUpdateWrapper<QmsStationDept> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(QmsStationDept::getStcd, stcd);

        repository.delete(wrapper);
    }

    /**
     * 新增站点与部门关联关系
     *
     * @param stcd 站点编码
     * @param deptId 部门ID
     * @return 成功返回null
     * @author QuCheng Tech
     * @since 2024/5/6
     */
    public String addStcdDept(String stcd, int deptId) {
        QmsStationDept addEn = new QmsStationDept();
        addEn.setStcd(stcd);
        addEn.setDid(deptId);

        repository.insert(addEn);

        return QCUnifyReturnValue.Success();
    }
}
